package com.study.services;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.LossJDao;
import com.study.model.mdao.InventoryMapper;
import com.study.model.mdao.LossMapper;
import com.study.model.mdao.OutboundMapper;
import com.study.model.mdao.ProductMapper;
import com.study.model.pojo.Loss;

@Service
public class LossService {
	@Autowired
	LossJDao ijDao;
	@Autowired
	ProductMapper pMapper;
	@Autowired
	InventoryMapper imMapper;
	@Autowired
	LossMapper lMapper;
	@Autowired
	OutboundMapper oMapper;
	@Autowired
	OutboundService oService;
	
	/**s
	 * 查询所有报损信息
	 */
	public List<Loss> findAll(){
		return ijDao.findAll();
	}
	
	/**
	 * 新增报损
	 * @param map 新增报损信息
	 * @param proId 产品id
	 */
	public String addloss(HashMap map,String proId,Loss loss) {
		int i=pMapper.findexist(proId);//是否存在
		String returnString;
		//判断是否存在，如果存在，执行报损
				if (i==1) {
					lMapper.addloss(map);//新增报损	
					returnString="y";
					return returnString;
				}else {
					returnString="n";
					return returnString;
				}	
	}
	
	
	/**
	 * 审核通过
	 * @param losId
	 */
	public void losspass(String losId) {
		lMapper.pass(losId);
		Loss loss=ijDao.findBylosId(losId);
		HashMap map1=new HashMap<>();
		map1.put("proId", loss.getProduct().getProId());
		map1.put("warName", loss.getWarName());
		int z=imMapper.findByproIdAndwarName(map1);//寻找符合条件的产品，判断是否存在
		if (z>0) {
			HashMap mapout=new HashMap<>();
			mapout.put("outId", "out0006");
			mapout.put("proId", loss.getProduct().getProId());
			mapout.put("saleId", losId);
			mapout.put("warName", loss.getWarName());
			mapout.put("intName", "报损出库");
			mapout.put("outNum", loss.getLosNum());
			mapout.put("outKind", "自费");
			mapout.put("outUse", "");
			mapout.put("outNote", loss.getLosNote());
			mapout.put("outStatic", "已审核");
			System.out.println("仓库名称:"+loss.getWarName()+"----数量:"+loss.getLosNum());
			//调用出库方法
			oService.outlossbound(mapout, loss.getProduct().getProId(), loss.getWarName(), loss.getLosNum());
		}
	}
	
	
	/**
	 * 删除报损信息
	 * @param losId
	 */
	public void delLoss(String losId) {
		lMapper.delLoss(losId);
	}
}
